package org.wheel.plugins.toolkit.sql.core;

import org.wheel.plugins.toolkit.sql.entity.BeanMeta;
import org.wheel.plugins.toolkit.sql.function.FunctionBuilder;
import org.wheel.plugins.toolkit.sql.helper.ColumnRef;
import org.wheel.plugins.toolkit.sql.helper.FieldMeta;
import org.wheel.plugins.toolkit.sql.render.DeleteSqlRenderer;
import org.wheel.plugins.toolkit.util.ReflectUtil;

public class LambdaDeleteWrapper<T> extends AbstractWrapper<T, LambdaDeleteWrapper<T>> {

    public LambdaDeleteWrapper(Class<T> clazz) {
        super(clazz);
    }

    public LambdaDeleteWrapper(Class<T> clazz, String tableAlias) {
        super(clazz, tableAlias);
    }


    /* ---------- 禁用 SELECT 相关能力 ---------- */
    @Override
    public LambdaDeleteWrapper<T> function(FunctionBuilder fn) {
        throw new UnsupportedOperationException("DELETE 语句不允许使用 SELECT 函数");
    }

    public <T> LambdaDeleteWrapper<T> delete(T bean){
        return delete(bean, true);
    }

    public <T> LambdaDeleteWrapper<T> delete(T bean, boolean ignoreNull) {
        BeanMeta beanMeta = BeanMeta.of(clazz);
        for (FieldMeta fieldMeta : beanMeta.getFields()) {
            Object val = ReflectUtil.getFieldValue(bean, fieldMeta.getFieldName());
            if(ignoreNull && val == null){
                continue;
            }
            this.eq(fieldMeta.getColumn(), val);
        }
        return (LambdaDeleteWrapper<T>) this;
    }

    /* ---------- 最终 SQL 出口 ---------- */
    @Override
    public String getSql() {
        /* 1. 使用专用渲染策略，只拼 DELETE 部分 */
        String sql = new DeleteSqlRenderer(dialect).render(tree);
        /* 2. 走公共参数渲染 */
        return renderSql(sql);
    }
}
